//
//  HWStatusFrame.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/13.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWStatusFrame.h"
#import "HWStatus.h"
#import "HWUser.h"
#import "NSString+Ectentsion.h"
#import "HWStatusPhotosView.h"
#define HWCellBorderWidth 10

@implementation HWStatusFrame
- (void)setStatus:(HWStatus *)status{
    _status = status;
    HWUser *user = status.user;
    /*头像**/
    CGFloat iconWH = 35;
    CGFloat iconX = HWCellBorderWidth;
    CGFloat iconY = HWCellBorderWidth;
    self.iconViewF = CGRectMake(iconX, iconY, iconWH, iconWH);
    /*昵称**/
    CGFloat nameX = CGRectGetMaxX(self.iconViewF) + HWCellBorderWidth;
    CGFloat nameY = CGRectGetMinY(self.iconViewF);
    
    CGSize nameSize = [self sizeWithString:user.name font:HWNameFont maxSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)];
    self.nameLabelF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    /*会员**/
    if (user.isVip) {
        CGFloat vipX = CGRectGetMaxX(self.nameLabelF) + HWCellBorderWidth;
        CGFloat vipY = iconY;
        CGFloat vipWidth = 14;
        CGFloat vipHeight = nameSize.height;
        self.vipViewF = CGRectMake(vipX, vipY, vipWidth, vipHeight);
    }
    /*时间**/
    CGFloat timeX = CGRectGetMinX(self.nameLabelF);
    CGFloat timeY = CGRectGetMaxY(self.nameLabelF) + HWCellBorderWidth;
    CGSize timeSize = [self sizeWithString:status.created_at font:HWCreat_atFont maxSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)];
    self.timeLabelF = CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
    /*来源**/
    CGFloat sourceX = CGRectGetMaxX(self.timeLabelF) + HWCellBorderWidth;
    CGFloat sourceY = CGRectGetMinY(self.timeLabelF);
    CGSize sourceSize = [self sizeWithString:status.source font:HWSourceFont maxSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)];
    self.sourceLabelF = CGRectMake(sourceX, sourceY, sourceSize.width, timeSize.height);

    /*正文**/
    CGFloat contentX = iconX ;
    CGFloat contentY = MAX(CGRectGetMaxY(self.iconViewF), CGRectGetMaxY(self.timeLabelF));
    CGSize contentSize = [self sizeWithString:status.text font:HWContentFont maxSize:CGSizeMake(SCREEN_WIDTH - 2*HWCellBorderWidth, MAXFLOAT)];

    self.contentLabelF = CGRectMake(contentX, contentY, contentSize.width, contentSize.height);
     /*配图**/
    CGFloat originalHeight = 0;
    if (status.pic_urls.count) {
//        CGFloat photosWH = 300;
        CGSize size = [HWStatusPhotosView sizeWithCount:status.pic_urls.count];
        CGFloat photosX = iconX;
        CGFloat photosY = CGRectGetMaxY(self.contentLabelF) + HWCellBorderWidth;
        self.photosViewF = CGRectMake( photosX, photosY, size.width, size.height);
        originalHeight = CGRectGetMaxY(self.photosViewF) + HWCellBorderWidth;

    }else{
        originalHeight = CGRectGetMaxY(self.contentLabelF) + HWCellBorderWidth;
    }
      /*原创微博**/
    CGFloat originalX = 0;
    CGFloat originalY = 0;
    CGFloat originalWidth = SCREEN_WIDTH;
    self.originalViewF = CGRectMake(originalX, originalY, originalWidth, originalHeight);
    /**转发正文*/
    if (status.retweeted_status) {
        CGFloat retweetContentX = iconX;
        CGFloat retweetContentY = HWCellBorderWidth;
        CGSize retweetContentSize = [self sizeWithString:status.retweeted_status.text font:HWRetweetContentFont maxSize:CGSizeMake(SCREEN_WIDTH - 2*HWCellBorderWidth, MAXFLOAT)];
        self.retweetContentLabelF = CGRectMake(retweetContentX, retweetContentY, retweetContentSize.width, retweetContentSize.height);
        /*配图**/
        CGFloat retweetViewHeight = 0;
    if (status.retweeted_status.pic_urls > 0) {
        CGSize size = [HWStatusPhotosView sizeWithCount:status.retweeted_status.pic_urls.count];
        CGFloat retweetPhotosX = iconX;
        CGFloat retweetPhotosY = CGRectGetMaxY(self.retweetContentLabelF) + HWCellBorderWidth;
        self.retweetPhotosViewF = CGRectMake(retweetPhotosX,retweetPhotosY, size.width, size.height);
        retweetViewHeight = CGRectGetMaxY(self.retweetPhotosViewF) + HWCellBorderWidth;
    }else{
        retweetViewHeight = CGRectGetMaxY(self.retweetContentLabelF) + HWCellBorderWidth;
    }
        /**转发微博*/
        CGFloat retweetViewX = 0;
        CGFloat retweetViewY = CGRectGetMaxY(self.originalViewF);
        CGFloat retweetViewWidth = SCREEN_WIDTH;
        self.retweetViewF = CGRectMake(retweetViewX, retweetViewY, retweetViewWidth, retweetViewHeight);
        CGFloat toolBarX = 0;
        CGFloat toolBarY = CGRectGetMaxY(self.retweetViewF);
        CGFloat toolBarWidth = SCREEN_WIDTH;
        CGFloat toolBarHeight = 36;
        self.toolBarViewF = CGRectMake(toolBarX, toolBarY, toolBarWidth, toolBarHeight);
    }else{
        CGFloat toolBarX = 0;
        CGFloat toolBarY = CGRectGetMaxY(self.originalViewF) + 1;
        CGFloat toolBarWidth = SCREEN_WIDTH;
        CGFloat toolBarHeight = 36;
         self.toolBarViewF = CGRectMake(toolBarX, toolBarY, toolBarWidth, toolBarHeight);
    }
    self.cellHeight = CGRectGetMaxY(self.toolBarViewF) + HWCellBorderWidth;
}
-(CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
@end
